﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BusinessObjects.BO_GetObjects;
using LibraryO2S;
using BusinessObjects;
using System.IO;

public partial class controls_admin_CategoryAdd : Pages_Base
{
    protected void Page_Load(object sender, EventArgs e)
    {
        base.removeHGC(divMessage);
        if (!IsPostBack)
        {
            try
            {
                loadSetting();
                base.OpenConnection();
                base.loadCategories(ddlParent, 0);
                if (Request["c_id"] != null)
                    ddlParent.SelectedValue = Request["c_id"].ToString();
                ddlParent_SelectedIndexChanged(null, null);
            }
            catch { }
            finally { base.CloseConnection(); }
        }
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        txtFullCategoryDescription.Text = "";
        txtFullCategoryName.Text = "";
        cboDisOnHome.Checked = true;
        cboMenu.Checked = true;
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            base.OpenConnection();
            DataTable adminDT = (DataTable)Session["Admin"];
            string categoryName = txtFullCategoryName.Text.Trim();
            string categoryDescription = txtFullCategoryDescription.Text.Trim();
            int sortdisplay = Convert.ToInt32(txtSortDisplay.Text.Trim());
            int parent = Convert.ToInt32(ddlParent.SelectedValue);
            string filename = String.Empty;
            try
            {
                if (fulIcon.HasFile)
                {
                    filename = fulIcon.FileName;
                    int count = 0;
                    while (File.Exists(Server.MapPath("~/upload/home/") + filename))
                    {
                        filename = count + "_" + fulIcon.FileName;
                        count++;
                    }
                    fulIcon.SaveAs(Server.MapPath("~/upload/home/") + filename);
                }
            }
            catch (Exception ex)
            {
                divMessage.InnerHtml = O2S_Message.Error("Hệ Thống", "Lỗi Ghi file! " + ex.Message);
                return;
            }
            string result = BO_Categories.Insert(
                    parent, "",
                    categoryName, categoryName,
                    "", filename, "", "", categoryDescription, "",
                    cboDisOnHome.Checked, cboMenu.Checked, sortdisplay,
                    DateTime.Now, adminDT.Rows[0]["USER_NAME"].ToString(),
                    DateTime.Now, "", true);
            if (result.Equals("0")) divMessage.InnerHtml = O2S_Message.Error("Thông Báo", "Chưa thực hiện được, vui lòng trở lại sau !");
            else
            {
                divMessage.InnerHtml = O2S_Message.Success("Thông Báo", "Thực hiện thành công !");
                btnAdd_Click(null, null);
            }

        }
        catch (Exception ex)
        {
            divMessage.InnerHtml = O2S_Message.Error("Lỗi", "Phát hiện lỗi: <br />" + ex.Message);
        }
        finally { base.CloseConnection(); }
    }
    protected void ddlParent_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            DataTable objDT = BO_Categories.BeforeInsert(Convert.ToInt32(ddlParent.SelectedValue));
            if (objDT != null && objDT.Rows.Count > 0)
                txtSortDisplay.Text = objDT.Rows[0]["SORT_DISPLAY"].ToString();
            else
                txtSortDisplay.Text = "1";
        }
        catch { txtSortDisplay.Text = "1"; }
    }
    private void loadSetting()
    {
        DataTable objDT = BO_Settings.SelectByGroupId(8, 1);
        foreach (DataRow dr in objDT.Rows)
        {
            string value = dr["SETTING_VALUE"].ToString();
            switch (dr["SETTING_KEY"].ToString())
            {
                case "NOTE_CATE_CATE":
                    NOTE_CATE_CATE.InnerText = value;
                    break;
                case "NOTE_CATE_DESCRIPT":
                    NOTE_CATE_DESCRIPT.InnerText = value;
                    break;
                case "NOTE_CATE_DISPLAY":
                    NOTE_CATE_DISPLAY.InnerText = value;
                    break;
                case "NOTE_CATE_ICON":
                    NOTE_CATE_ICON.InnerText = value;
                    break;
                case "NOTE_CATE_MENU":
                    NOTE_CATE_MENU.InnerText = value;
                    break;
                case "NOTE_CATE_SORT":
                    NOTE_CATE_SORT.InnerText = value;
                    break;
                case "NOTE_CATE_TITLE":
                    NOTE_CATE_TITLE.InnerText = value;
                    break;
            }
        }
    }
}
